package com.example.networkclass;

import android.os.Bundle;
import android.text.NoCopySpan;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

public class MainActivity extends AppCompatActivity {

    private RecyclerView listRv;
    private String[] names = {"小猫","哈士奇","小黄鸭","小鹿","老虎"};
    private int[] icons = {R.drawable.cat,R.drawable.siberiankusky,
            R.drawable.yellowduck,R.drawable.fawn,R.drawable.tiger,};
    private String[] introduces = {
            "猫，属于猫科动物，分家猫、野猫，是全世界家庭中较为广泛的宠物。",
            "西伯利亚雪橇犬，常见别名哈士奇，昵称为二哈。",
            "鸭的体型相对较小，颈短，一些属的嘴要大些。腿位于身体后方，因而步态蹒跚。",
            "鹿科是哺乳纲偶蹄目下的一科动物。体型大小不等，为有角的反刍类。",
            "虎，大型猫科动物;毛色浅黄或棕黄色，满有黑色横纹;头圆、耳短，耳背面黑色，中央有一白斑甚显著:四肢健壮有力;尾粗长，具黑色环纹，尾端黑色."
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        listRv = findViewById(R.id.recyclerView);
        listRv.setLayoutManager(new LinearLayoutManager(this,LinearLayoutManager.VERTICAL, false));
        listRv.setAdapter(new MyAdapter());

        //设置点击事件，创建接口
        onItemClickListener = new OnItemClickListener() {
            @Override
            public void onItemClick(int position) {
                Toast.makeText(MainActivity.this, "点击了"+names[position], Toast.LENGTH_SHORT).show();
            }
        };
    }

    //创建adapter
    class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder>{


        //创建viewholder
        @NonNull
        @Override
        public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            View view = View.inflate(MainActivity.this, R.layout.item_list, null);
            return new ViewHolder(view);
        }

        //绑定数据
        @Override
        public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
            //图片
            holder.iconIv.setImageResource(icons[position]);
            //名字
            holder.nameTv.setText(names[position]);
            //介绍
            holder.introduceTv.setText(introduces[position]);

            //添加点击事件
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if(onItemClickListener != null){
                        onItemClickListener.onItemClick(position);
                    }
                }
            });

        }


        @Override
        public int getItemCount() {
            return names.length;
        }

        class ViewHolder extends RecyclerView.ViewHolder{

            ImageView iconIv;
            TextView nameTv,introduceTv;

            public ViewHolder(@NonNull View itemView) {
                super(itemView);
                iconIv = itemView.findViewById(R.id.iv);
                nameTv = itemView.findViewById(R.id.name);
                introduceTv = itemView.findViewById(R.id.introduce);
            }
        }

    }

    private OnItemClickListener onItemClickListener;
    interface OnItemClickListener{
        void onItemClick(int position);
    }
}
